#include "mainproc.h"
#include <QDebug>

MainProc::MainProc():Process()
{
    name = "MainProc";
}

MainProc::~MainProc()
{
}



void MainProc::Run()
{
    LogManager::getSingleton().Message("MainProc", "step "+LogManager::getSingleton().toStr(currentStep));
    switch(currentStep){
    case 0:{
        ResourceAllocator::getSingleton().askForResource("ProgramInBuffer",id);
    }
    break;
    case 1:{

        jobGovernorId = -1;
        Resource* programBufferResource = usingResourceList.at(usingResourceList.size()-1);
        ElementsBase* elements = programBufferResource->getElement();
        programBuffer = elements->at(0);
        String runningTime = elements->at(1);

        ResourceAllocator::getSingleton().destroyResource(programBufferResource);

        std::istringstream buffer(runningTime);
        buffer >> runningTimeInt;

        if(runningTimeInt != 0) {
            currentStep = 1;
        }else {
            String jobGovernorStr = elements->at(2);
            std::istringstream jgbuffer(jobGovernorStr);
            jgbuffer >> jobGovernorId;
            currentStep = 2;
        }
    }
    break;
    case 2:{
        Planner::getSingleton().createProcess(id, READY, 50, new JobGovenor());
        currentStep=-1;
    }
    break;
    case 3:{
        if(jobGovernorId == -1) {
            LogManager::getSingleton().Error("MainProc", "wrong jobGovernor id");
        }else {
            Planner::getSingleton().deleteProcess(jobGovernorId);
        }
        currentStep=-1;
    }
    break;
    default:
        LogManager::getSingleton().Error("MainProc", "reached default step");
    }
    currentStep++;
}
